//
//  MZNiceNotesViewController.m
//  OurTourism
//
//  Created by 孟镇 on 15/12/3.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "MZNiceNotesViewController.h"
#import "MZNiceNotesTableViewCell.h"
#import "Header.h"
#import "YBTraveldetialViewController.h"

@interface MZNiceNotesViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *modelItems;
@property (nonatomic, strong)MBProgressHUD *HUB;

@end

@implementation MZNiceNotesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    self.navigationItem.title = self.tempModel.name;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 25, 25);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-iconfonticonfontjiantouzuo11@2x"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    [self getData];
    [self createTableView];
    
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUB.labelText = @"正在加载";
    [self.HUB show:YES];
}

#pragma mark - 获取数据
- (void)getData
{
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/trips/", self.tempModel.type, self.tempModel.valueId];
    [MZNetHandle getUrl:url cookie:nil result:^(id result) {
        
        self.modelItems = [NSMutableArray array];
        self.modelItems = [MZRecommendModel transformWithArray:result[@"items"]];
        
        [self.HUB removeFromSuperview];
        [self.tableView reloadData];
        
    }];
    
}


#pragma mark - 创建tableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    [self.tableView registerClass:[MZNiceNotesTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
}
#pragma mark - 自定义按钮触发方法
- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MZNiceNotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    cell.niceModel = self.modelItems[indexPath.row];
    
    return cell;
    
}


#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YBTraveldetialViewController *tdVC = [[YBTraveldetialViewController alloc] init];
    tdVC.valueId = (NSString *)[self.modelItems[indexPath.row] valueId];
    tdVC.whoPush = @"mz";
    [self presentViewController:tdVC animated:YES completion:^{
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WIDTH / 3 + 20 + 20;
}


- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
